 <?php
 /**
 * Joomla! 1.5 component PB-Roster
 *
 * @version $Id: pbroster.php 2010-09-10 10:03:33 svn $
 * @author ML@pixelbyte.dk
 * @package Joomla
 * @subpackage PB-Roster
 * @copyright (C) 2010- pixelbyte.dk
 * @license GNU/GPL
 *
 * A World of Warcraft Guild Roster
 *
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class HTML_AdminRoster {

  /**
   * Attempt to make some sort of version control so that both Joomla 1.5 and 1.6 works
   * without the need to make different versions for each.
   */
  function getAdminRights() {
    // Check Joomla version
    $version = new JVersion;
	$j_version = substr($version->getShortVersion(), 0, 3);
	
	if ($j_version == "1.6")
	  return HTML_AdminRoster::getAdminRights16();
	else
	  return HTML_AdminRoster::getAdminRights15();
  }
  
  // Joomla 1.5
  function getAdminRights15() {
    $user =& JFactory::getUser();
    if ($user->gid > 18)
	  return true;
	else
	  return false;
  }
  
  // Joomla 1.6
  function getAdminRights16() {
    $user =& JFactory::getUser();

	if ($user->authorize( 'com_content', 'edit', 'content', 'all' ))
	  return true;
	else
	  return false;
  }
  
  function includeAdminDocs() {
    $document = & JFactory :: getDocument();
	$document->addStyleSheet(getRosterPath("roster").'css/main.css');
	$document->addStyleSheet(getRosterPath("roster").'css/guild_roster.css');
	$document->addStyleSheet(getRosterPath("roster").'css/guild_account.css');
	$document->addScript(getRosterPath("roster").'js/jquery-1.4.2.min.js');
	$document->addScript(getRosterPath("roster").'js/faq.js');
  }
  
  function includeAdminMemberDocs() {
    $document = & JFactory :: getDocument();
	$document->addScript(getRosterPath("roster").'js/core.js');
	$str2 = '&name='.urlencode(JText::_( 'COM_PBROSTER_WOW_CHARACTER_NAME' )).'&level='.urlencode(JText::_( 'COM_PBROSTER_WOW_LEVEL' )).'&race='.urlencode(JText::_( 'COM_PBROSTER_WOW_RACE' )).'&class='.urlencode(JText::_( 'COM_PBROSTER_WOW_CLASS' )).'&path='.getRosterPath("images");
	$content = 'function showUser(str) {
		if (str=="") {
		  document.getElementById("txtMember").innerHTML="";
		  return;
		}
		if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		}
		else { // code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function() {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
		    document.getElementById("txtMember").innerHTML=xmlhttp.responseText;
		  }
		}
		xmlhttp.open("GET","'.getRosterPath("roster").'getuser.php?n="+str+"'.$str2.'",true);
		xmlhttp.send();
	} 
	';
	$document->addScriptDeclaration( $content );
  }
  
  function headerAdminMenu($config, $active) {
    $enabled = 0;
	if (getPlugin('char_roster')) {
	  $enabled = enabledPluginCRoster();
	}
    echo '<ul class="profile-sidebar-menu" id="profile-sidebar-menu">';
    if ($config->menu_roster == 'yes') {
      echo '<li class="'.($active == 'roster' ? 'active' : '').'"><a title="'.JText::_( 'COM_PBROSTER_MENU_ITEM_ROSTER' ).'" href="'.JRoute::_('index.php?option=com_pbroster&view=pbroster').'"><span class="arrow"><span class="icon">'.JText::_( 'COM_PBROSTER_MENU_ITEM_ROSTER' ).'</span></span></a></li>'."\n";
    }
	if ($enabled) {
	  echo '<li class="'.($active == 'account' ? 'active' : '').'"><a title="'.JText::_( 'COM_PBROSTER_MENU_ITEM_ACCOUNT' ).'" href="'.JRoute::_('index.php?option=com_pbroster&view=account').'" class="has-submenu" rel="np"><span class="arrow"><span class="icon">'.JText::_( 'COM_PBROSTER_MENU_ITEM_ACCOUNT' ).'</span></span></a></li>'."\n";
    }
	echo '<li class="'.($active == 'admin' ? 'active' : '').'"><a title="'.JText::_( 'COM_PBROSTER_MENU_ITEM_ADMIN' ).'" href="'.JRoute::_('index.php?option=com_pbroster&view=admin&page=member').'" class="vault" rel="np"><span class="arrow"><span class="icon">'.JText::_( 'COM_PBROSTER_MENU_ITEM_ADMIN' ).'</span></span></a></li>'."\n";
  }

  function adminFooter() {
    $db =& JFactory::getDBO();
    $query = "SELECT * FROM #__guildroster_main";
    $db->setQuery($query);

	$lostTime = 0;
    $cache = $db->loadObject();
    if( $db->getErrorNum() || !isset($cache) || !isset($cache->cache_date))
	  $lostTime = 1;
	
	echo '<div class="guild_roster_footer">';
	echo '</div></div></div>';
  }
  
  function footerButtons() {
    echo '</div></div>'; //rosterHeader()
  }

  function showMemberAdmin($rows, $option) {
	$account =& JFactory::getUser();
    HTML_AdminRoster::includeAdminDocs();
	HTML_AdminRoster::includeAdminMemberDocs();
	$config = getGuildConfig();
	
	// Modify breadcrumbs
	$mainframe = &JFactory::getApplication();
	$app =& JFactory::getApplication();
	$pathway =& $app->getPathway();
	$pathway->addItem(JText::_( 'COM_PBROSTER_MENU_ITEM_ADMIN' ).' @ '.ucfirst($account->name));
	
	$ginfo = getGuildInfo();
	foreach ( $ginfo as $guild ) {
	  // Get faction
	  if ($guild->faction == 0)
	    $faction = "alliance";
	  else
	    $faction = "horde";
		
	  $guildnote = $guild->guild_info;
	}
	
	// Check template setting
    $template = getRosterTemplateAdjust($config);

?>	
<div class="RosterPage" style="width:<?php echo $config->roster_width; ?>px;<?php echo $template->marginleft.$template->margintop; ?>">
  <div class="content-top">
  <div class="content-trail"> </div>
  <div class="content-bot">
	<div id="profile-wrapper" class="profile-wrapper profile-wrapper-<?php echo $faction; ?>">
	  <div class="profile-sidebar-anchor">
		<div class="profile-sidebar-outer">
		  <div class="profile-sidebar-inner">
			<div class="profile-sidebar-contents">
			  <div class="profile-sidebar-crest">
				<?php HTML_AdminRoster::headerAdminMenu($config, "admin"); ?>
			  </div>
			</div>
		  </div>
		</div>
	  </div>
	  <div class="profile-contents">
		<div class="profile-section-header">
		  <h3 class="category admin"><?php echo JText::_( 'COM_PBROSTER_ADMIN_PAGE_TITLE' ); ?></h3>
		</div>
		<div class="profile-section events">
		
		<!-- Update Guildroster -->
		<li class="event-category category-secondary">
		  <h3 class="category admin"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_TITLE_UPDATE_GUILD' ); ?></h3>
		  <div class="profile-box-simple"></div>
		  <ul>
		    <li class="empty"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_DESC_UPDATE_GUILD' ); ?></li>
		  </ul>
		  <li class="event-summary">
		    <p>
			  <form id="form_update" action="<?php echo JRoute::_('index.php?option=com_pbroster&view=pbroster'); ?>" method="post">
				<button class="update update_guild" title="<?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_UPDATE_GUILD' ); ?>" type="submit" name="force_roster_update" value="Update"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_UPDATE_GUILD' ); ?></button>
			  </form>
			</p>
		  </li>
		</li>
		<span class="clear"><!-- --></span>
		<span style="height:20px">&nbsp;</span>
		
		<!-- Guild Notes -->		
		<li class="event-category category-secondary">
		  <h3 class="category admin"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_TITLE_GUILD_NOTE' ); ?></h3>
		  <div class="profile-box-simple"></div>
		  <ul>
		    <li class="empty"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_DESC_GUILD_NOTE' ); ?></li>
		  </ul>
		  <form method="post" action="<?php echo JRoute::_('index.php?option=com_pbroster&view=admin&page=save_guildnote'); ?>" id="form">
		    <li class="event-summary system-event">
		      <p>
			    <textarea class="input guildnote" tabindex="13" maxlength="320" size="50" name="guildnote" id="guildnote" type="text"><?php echo $guildnote; ?></textarea>
			  </p>
		    </li>
		    <span class="event-summary account-form">
			  <button type="submit" class="button" value="<?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_UPDATE_NOTE' ); ?>" name="post" tabindex="14"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_UPDATE_NOTE' ); ?></button>
		    </span>
		  </form>
		</li>
		<span class="clear"><!-- --></span>
		<span style="height:20px">&nbsp;</span>
		
		<!-- Remove Member -->
		<li class="event-category category-secondary">
		  <h3 class="category admin"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_TITLE_REMOVE_MEMBER' ); ?></h3>
		  <div class="profile-box-simple"></div>
		  <ul>
		    <li class="empty"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_DESC_REMOVE_MEMBER' ); ?></li>
		  </ul>
		  
		  <li class="event-summary">
		    <p>
			  <form id="form" action="<?php echo JRoute::_('index.php?option=com_pbroster&view=admin&page=del_member'); ?>" method="post">
				<label class="label remove-member" for="lookup_members"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_LABEL_TITLE_REMOVE_MEMBER' ); ?>
				  <span class="small">
					<?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_LABEL_DESC_REMOVE_MEMBER' ); ?>
				  </span>
				</label>
				<select class="select member" onkeyup="showUser(this.value);" onChange="showUser(this.value)" name="lookup_members" id="lookup_members" />
				  <option value=" "><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_SELECT_REMOVE_MEMBER' ); ?></option>

<?php
				  $cMem = getMemberInfo();
				  $i=0;
				  foreach ($cMem as $mem) {
				    echo '<option value="'.urlencode($mem->name).'&l='.$mem->level.'&r='.$mem->raceId.'&lr='.urlencode(return_race_lang($mem->raceId)).'&g='.$mem->genderId.'&lg='.urlencode(return_gender_lang($mem->genderId)).'&c='.$mem->classId.'&lc='.urlencode(return_class_lang($mem->classId)).'">'.$mem->name.'</option>\n';
				    $i++;
				  }
?>

				</select>
				<div id="txtMember">
				  <div class="adminGetUser_wrapper">
				    <table cellpadding="0" cellspacing="0" border="0" id="table" class="adminGetUserTable">
					  <tr>
					    <th width="55px"><?php echo JText::_( 'COM_PBROSTER_WOW_LEVEL' ); ?></th>
					    <th><?php echo JText::_( 'COM_PBROSTER_WOW_CHARACTER_NAME' ); ?></th>
					    <th width="50px"><?php echo JText::_( 'COM_PBROSTER_WOW_RACE' ); ?></th>
					    <th width="50px"><?php echo JText::_( 'COM_PBROSTER_WOW_CLASS' ); ?></th>
					  </tr>

					  <tr>
					    <td class="getuser">&nbsp;</td>
					    <td class="getuser">&nbsp;</td>
					    <td class="getuser">&nbsp;</td>
					    <td class="getuser">&nbsp;</td>
					  </tr>
				    </table>
				  </div>
			    </div>
			    <div id="txtMember_total"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_DESC_REMOVE_MEMBER' ).' '.$i; ?></div>
				<button title="<?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_REMOVE_MEMBER' ); ?>" id="delete_member" name="delete_member" value="Delete" type="submit"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_REMOVE_MEMBER' ); ?></button>
			  </form>
			</p>
		  </li>
		</li>
		<span class="clear"><!-- --></span>
		<span style="height:20px">&nbsp;</span>
		
		<!-- Add Member -->
		<li class="event-category category-secondary">
		  <h3 class="category admin"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_TITLE_ADD_MEMBER' ); ?></h3>
		  <div class="profile-box-simple"></div>
		  <ul>
		    <li class="empty"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_DESC_ADD_MEMBER' ); ?></li>
		  </ul>
		  <li class="event-summary">
		    <p>
			  <form id="form" action="<?php echo JRoute::_('index.php?option=com_pbroster&view=admin&page=add_member'); ?>" method="post">
				<label class="select add-member" for="addmember"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_LABEL_TITLE_ADD_MEMBER' ); ?>
				  <span class="small"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_LABEL_DESC_ADD_MEMBER' ); ?></span>
				</label>
				<input class="input add_member" type="text" maxlength=15 name="addmember" id="addmember" />
				<div style="height:50px;">&nbsp;</div>
				<button title="<?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_ADD_MEMBER' ); ?>" id="add_member" name="add_member" value="Submit" type="submit"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_ADD_MEMBER' ); ?></button>
			  </form>
			</p>
		  </li>
		</li>
		<span class="clear"><!-- --></span>
		<span style="height:20px">&nbsp;</span>
	
		<!-- Update Members -->
		<li class="event-category category-secondary">
		<h3 class="category admin"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_TITLE_UPDATE_MEMBERS' ); ?></h3>
		  <div class="profile-box-simple"></div>
		  <ul>
		    <li class="empty"><?php echo JText::_( 'COM_PBROSTER_ADMIN_SUB_DESC_UPDATE_MEMBERS' ); ?></li>
		  </ul>
		  <li class="event-summary">
		    <p>
			  <form id="form" action="<?php echo JRoute::_('index.php?option=com_pbroster&view=admin&page=update_mlist'); ?>" method="post">
				<!-- One character takes about 3-4sec (3.5sec) with throttle and data retrival -->
				<div><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_LABEL_DESC_UPDATE_MEMBERS_TIME' ).': '.secondsToWords($i*3.5); ?></div>
				<button class="update update_guild" title="<?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_UPDATE_MEMBERS' ); ?>" id="update_mlist" name="update_mlist" value="Update" type="submit"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FORM_BUTTON_UPDATE_MEMBERS' ); ?></button>
			  </form>
			</p>
		  </li>
		</li>
		<span class="clear"><!-- --></span>
		<span style="height:20px">&nbsp;</span>
		
		<!-- FAQ -->
		<div id="adminfaq">
		  <div class="faqslide" title="<?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_TOGGLE' ); ?>" style="cursor: pointer;">
		    <h1><?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_TITLE' ); ?></h1>
		    <div id="faq_showhide"><?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_DESC' ); ?></div>
		  </div>
		  <p>
		  </p>
		</div>
		<div id="adminfaq_text" class="faqview">
		  <h1><?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_TITLE_REMOVE_MEMBER' ); ?></h1>
		  <?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_DESC_REMOVE_MEMBER' ); ?>
		  <h1><br /><br /><?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_TITLE_ADD_MEMBER' ); ?></h1>
		  <?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_DESC_ADD_MEMBER' ); ?>
		  <h1><br /><br /><?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_TITLE_UPDATE_MEMBER' ); ?></h1>
		  <?php echo JText::_( 'COM_PBROSTER_ADMIN_FAQ_DESC_UPDATE_MEMBER' ); ?>
		  <br /><br />
		</div>

		</li>
		  <span class="clear"><!-- --></span>
		</div>
	  </div>
	  <span class="clear"><!-- --></span>
	</div>
  </div>
  </div>
</div>

<?php
  }

}


?>